package com.jingdogn.controller;

import com.jingdogn.dao.GoodRepository;
import com.jingdogn.entity.Good;
import com.jingdogn.utils.JDShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/")
public class IndexController {

    @Autowired
    GoodRepository goodRepository;
    @Autowired
    JDShop jdShop;

    @GetMapping("/save")
    public String save() {
        Good good = new Good();
//        good.setPrice("111");
        goodRepository.save(good);
        return "保存成功";
    }

    @GetMapping("/get")
    public Good get(String id) {
        return goodRepository.findById(id).orElse(new Good());

    }

    @GetMapping("/list")
    public Iterable<Good> list() {
        return goodRepository.findAll();
    }

    @GetMapping("/initialize")
    public String initialize(String key, Integer maxPage) {
        Date startDate = new Date();
        List<Good> shopList = jdShop.getShopList(maxPage, key);
        for (Good good : shopList) {
            goodRepository.save(good);
        }
        Date endData = new Date();
        return "结束:" + (endData.getTime() - startDate.getTime());
    }
}
